<?php
class CategoriesController extends AppController {
	var $name = 'Categories';
	var $components = array ('Auth', 'Session');
	var $helpers = array('Tinymce');
	var $uses = array('Category');
	/*
	 * get sub category by ajax
	 * 
	 * @param parentId int
	 * return void
	 */
	function getSubCategory($parentId){
		$this->layout = 'ajax';
		$subCategories = $this->Category->find('list',array(
			'recursive' => -1,
			'conditions' => array('Category.parent_id' => $parentId)
		));
		$this->set(compact('subCategories'));
	}
	
	/**
	 * Create new Category
	 * @author Tucq
	 * Added 27-06-2012
	 */
	function admin_add() {
		$this->set('title_for_layout', __('Create new Category'));
		
		if (!empty($this->request->data)) {
			//Process with submited data
			$this->Category->create();
			if ($this->Category->save($this->request->data)) {
				$this->Session->setFlash(__('Category saved successful'), 
					'default', 
					array('class' => 'alert alert-success')
				);
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Category could not be saved. Please, try again!'), 
					'default', 
					array('class' => 'alert alert-error'));
			}
		}
		
		//Get parent list => Get all categories
		$parents = $this->Category->find('all', array(
			'recursive' => -1,
			'fields' => array('id', 'title','parent_id'),
			'order' => array('parent_id')
				
		));
		$parents = Set::combine($parents, '{n}.Category.id', '{n}');

		$parentOptions = array();
		$listParents = array();
		foreach ($parents as $parent) {
			$listParents[$parent['Category']['parent_id']][] = $parent;
		}
		$parentOptions = $this->__searchListParent($listParents);
			
		$this->set(compact('parentOptions'));
	}
	
	function __searchListParent($listParents, $parent = 0, $level = 0, $result = array()) {
		$space = '&nbsp;';
		$str = '';
		if (isset($listParents[$parent]) && count($listParents[$parent])){
	   		if($level != 0) {
	   			for ($x = 0; $x <= $level; $x++) {
	    			$str .= $space;
	   			}
	   		}
			foreach($listParents[$parent] as $node) {
				$result[$node['Category']['id']] = $str.$node['Category']['title'];
				$result = $this->__searchListParent($listParents, $node['Category']['id'], $level + 1, $result);
			}
		}
		return $result;
	}
	
	
	/**
	 * List of Categories
	 * @author Tucq
	 * Added 27-06-2012
	 */
	function admin_index() {
		$this->set('title_for_layout', __('List Categories'));
		
		$this->paginate = array(
			'recursive' => -1, 
			'limit' => 10,
			'order' => 'parent_id'
		);
		$categories = $this->paginate('Category');
		$categories = Set::combine($categories, '{n}.Category.id', '{n}');
		$this->set(compact('categories'));
	}

	/**
	 * Edit Category
	 * @author Tucq
	 * Added 02-07-2012
	 */
	function admin_edit($id = null) {
		$this->set('title_for_layout',__('Edit Category'));
		if (!empty($this->request->data)) {
			if ($this->request->data['Category']['parent_id'] == $id) {
				$this->Session->setFlash(__('Invalid parent Category. Please try again'),
					'default',
					array('class' => 'alert alert-error')
				);
			} else {
				$this->Category->id = $id;
				if ($this->Category->save($this->request->data)) {
					$this->Session->setFlash(__('Update Category success'), 
						'default', 
						array('class' => 'alert alert-success')
					);
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('Update Category failed! Please try again.'), 
						'default', 
						array('class' => 'alert alert-error')
					);
				}
			}
		}
		
		//Get current category record info
		$this->request->data = $this->Category->find('first', array(
				'conditions' => array('id' => $id),
				'recursive' => -1
		));
		
		if (empty($this->request->data)) {
			$this->Session->setFlash(__('Invalid Category'), 
				'default', 
				array('class' => 'alert alert-error'));
			$this->redirect(array('action' => 'index'));
		}
		
		
		//Get parent list => Get all categories
		$parents = $this->Category->find('all', array(
				'recursive' => -1,
				'fields' => array('id', 'title','parent_id'),
				'order' => array('parent_id')
		
		));
		$parents = Set::combine($parents, '{n}.Category.id', '{n}');
		
		//Process parentOption
		$parentOptions = array();
		$listParents = array();
		foreach ($parents as $parent) {
			$listParents[$parent['Category']['parent_id']][] = $parent;
		}
		$parentOptions = $this->__searchListParent($listParents);
		$this->set(compact('parentOptions'));
	}
	
	
	/**
	 * Delete Category
	 * Allow delete category that has no children
	 * @author Tucq
	 * Added 02-07-2012
	 */
	function admin_delete($id = null) {
		$this->autoRender = false;
		if (!$id) {
			$this->Session->setFlash(__('Invalid Category'), 'default', array('class' => 'alert alert-error'));
			$this->redirect(array('action' => 'index'));
		}
		
		//Check children of current Category
		$children = $this->Category->find('count', array(
			'recursive' => -1, 
			'conditions' => array('parent_id' => $id)
		));
		
		if ($children) {
			//Has children
			$this->Session->setFlash(__('Could not delete that Category! Delete all its children first.').$children, 
				'default', 
				array('class' => 'alert alert-error'));
		} else {
			//No children
			if ($this->Category->delete($id)) {
				$this->Session->setFlash(__('Delete Category success! Please try again.'), 
					'default', 
					array('class' => 'alert alert-success'));
			} else {
				$this->Session->setFlash(__('Could not Category! Please try again.'), 
					'default', 
					array('class' => 'alert alert-error'));
			}
		}
		$this->redirect(array('action' => 'index'));
	}
	
	/**
	 * Set Category to Manager
	 */
	function admin_assign() {
		$this->set('title_for_layout', __('Set Category to Manager'));
		
	}
}
?>